package com.zshan.clinic.web.controller.patient;


import com.zshan.clinic.admin.entity.dto.MemberRechargeDTO;
import com.zshan.clinic.admin.entity.dto.MemberRefundDTO;
import com.zshan.clinic.admin.entity.vo.PatientMemberGearVo;
import com.zshan.clinic.admin.entity.vo.PatientMemberRechargeOrderVo;
import com.zshan.clinic.admin.entity.vo.PatientMemberTransVo;
import com.zshan.clinic.admin.entity.vo.PatientMemberVo;
import com.zshan.clinic.admin.service.patient.ClinicMemberService;
import com.zshan.clinic.common.response.RestResp;
import com.zshan.clinic.common.util.page.PageInfo;
import com.zshan.clinic.database.model.PatientMemberGear;
import com.zshan.clinic.web.controller.AbstractController;
import com.zshan.clinic.web.entity.UserInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


/**
 * 会员卡
 */
@RestController
@RequestMapping("/member")
public class MemberController extends AbstractController {


    @Autowired
    private ClinicMemberService memberService;

    /**
     * 收费：查询患者会员卡余额
     * @param patientId 患者ID
     * @return
     */
    @GetMapping("/getMemberBalance")
    public RestResp<PatientMemberVo> getMemberBalance(@RequestParam(name = "patientId") Long patientId) {
        UserInfo userInfo = getUserInfo();
        PatientMemberVo patientMemberVo = memberService.getMemberBalance(userInfo.getClinicId(), patientId);
        return RestResp.SUCCESS(patientMemberVo);
    }

    /**
     * 查询会员卡交易流水
     * @param patientId 患者ID
     * @param transNo 交易流水号
     * @param transType 交易类型 MemberTransTypeEnum
     * @param pageNo
     * @param pageSize
     * @return
     */
    @GetMapping("/getTransList")
    public RestResp<PageInfo<PatientMemberTransVo>> getTransList(@RequestParam(name = "patientId") Long patientId,
                                                                 @RequestParam(name = "transNo", required = false) String transNo,
                                                                 @RequestParam(name = "transType", required = false) String transType,
                                                                 @RequestParam(name = "pageNo", defaultValue = "1", required = false) Integer pageNo,
                                                                 @RequestParam(name = "pageSize", defaultValue = "10", required = false) Integer pageSize) {
        UserInfo userInfo = getUserInfo();
        PageInfo<PatientMemberTransVo> transList = memberService.getTransList(userInfo.getClinicId(), patientId,transNo,transType, pageNo, pageSize);
        return RestResp.SUCCESS(transList);
    }

    /**
     * 查询会员卡充值列表
     * @param patientId 会员ID
     * @param orderNo 订单号
     * @param orderStatus 订单状态 orderStatusEnum
     * @param pageNo
     * @param pageSize
     * @return
     */
    @GetMapping("/getRechargeList")
    public RestResp<PageInfo<PatientMemberRechargeOrderVo>> getRechargeList(@RequestParam(name = "patientId") Long patientId,
                                                                            @RequestParam(name = "orderNo", required = false) String orderNo,
                                                                            @RequestParam(name = "orderStatus", required = false) String orderStatus,
                                                                            @RequestParam(name = "pageNo", defaultValue = "1", required = false) Integer pageNo,
                                                                            @RequestParam(name = "pageSize", defaultValue = "10", required = false) Integer pageSize) {
        UserInfo userInfo = getUserInfo();
        PageInfo<PatientMemberRechargeOrderVo> patientList = memberService.getRechargeList(userInfo.getClinicId(), patientId,orderNo,orderStatus, pageNo, pageSize);
        return RestResp.SUCCESS(patientList);
    }

    /**
     * 会员卡充值
     * @param memberRechargeDTO
     * @return
     */
    @PostMapping("/recharge")
    public RestResp<Void> recharge(@RequestBody MemberRechargeDTO memberRechargeDTO) {
        UserInfo userInfo = getUserInfo();
        memberRechargeDTO.setCreateUserId(userInfo.getEmpId());
        memberRechargeDTO.setCreateBy(userInfo.getEmpName());
        memberRechargeDTO.setClinicId(userInfo.getClinicId());
        memberService.recharge(memberRechargeDTO);
        return RestResp.SUCCESS();
    }

    /**
     * 会员卡退款
     * @param memberRefundDTO
     * @return
     */
    @PostMapping("/refund")
    public RestResp<Void> refund(@RequestBody MemberRefundDTO memberRefundDTO) {
        UserInfo userInfo = getUserInfo();
        memberRefundDTO.setCreateUserId(userInfo.getEmpId());
        memberRefundDTO.setCreateBy(userInfo.getEmpName());
        memberRefundDTO.setClinicId(userInfo.getClinicId());
        memberService.refund(memberRefundDTO);
        return RestResp.SUCCESS();
    }

    /**
     * 查询充值档位列表
     * @param gearName 档位名称
     * @param pageNo
     * @param pageSize
     * @return
     */
    @GetMapping("/getGearList")
    public RestResp<PageInfo<PatientMemberGearVo>> getGearList(@RequestParam(name = "gearName", required = false) String gearName,
                                                               @RequestParam(name = "pageNo", defaultValue = "1", required = false) Integer pageNo,
                                                               @RequestParam(name = "pageSize", defaultValue = "10", required = false) Integer pageSize) {
        UserInfo userInfo = getUserInfo();
        PageInfo<PatientMemberGearVo> gearList = memberService.getGearList(userInfo.getClinicId(), gearName, pageNo, pageSize);
        return RestResp.SUCCESS(gearList);
    }

    /**
     * 添加充值档位
     * @return
     */
    @PostMapping("/addGear")
    public RestResp<Void> addGear(@RequestBody PatientMemberGear gear) {
        UserInfo userInfo = getUserInfo();
        gear.setCreateUserId(userInfo.getEmpId());
        gear.setCreateBy(userInfo.getEmpName());
        gear.setClinicId(userInfo.getClinicId());
        memberService.addGear(gear);
        return RestResp.SUCCESS();
    }

    /**
     * 编辑充值档位
     * @return
     */
    @PostMapping("/updateGear")
    public RestResp<Void> updateGear(@RequestBody PatientMemberGear gear) {
        UserInfo userInfo = getUserInfo();
        gear.setUpdateUserId(userInfo.getEmpId());
        gear.setUpdateBy(userInfo.getEmpName());
        gear.setClinicId(userInfo.getClinicId());
        memberService.updateGear(gear);
        return RestResp.SUCCESS();
    }

    /**
     * 删除充值档位
     * @return
     */
    @PostMapping("/deleteGear")
    public RestResp<Void> deleteGear(@RequestBody PatientMemberGear gear) {
        UserInfo userInfo = getUserInfo();
        gear.setUpdateUserId(userInfo.getEmpId());
        gear.setUpdateBy(userInfo.getEmpName());
        gear.setClinicId(userInfo.getClinicId());
        memberService.deleteGear(gear);
        return RestResp.SUCCESS();
    }

    /**
     * 启用充值档位
     * @return
     */
    @PostMapping("/enableGear")
    public RestResp<Void> enableGear(@RequestBody PatientMemberGear gear) {
        UserInfo userInfo = getUserInfo();
        gear.setUpdateUserId(userInfo.getEmpId());
        gear.setUpdateBy(userInfo.getEmpName());
        gear.setClinicId(userInfo.getClinicId());
        memberService.enableGear(gear);
        return RestResp.SUCCESS();
    }

    /**
     * 停用充值档位
     * @return
     */
    @PostMapping("/disableGear")
    public RestResp<Void> disableGear(@RequestBody PatientMemberGear gear) {
        UserInfo userInfo = getUserInfo();
        gear.setUpdateUserId(userInfo.getEmpId());
        gear.setUpdateBy(userInfo.getEmpName());
        gear.setClinicId(userInfo.getClinicId());
        memberService.disableGear(gear);
        return RestResp.SUCCESS();
    }
}
